$(document).ready(function() {

        var dialogClass = 'view_new_attribute';
        var dialogTitle = 'New Attribute';
        var formToSubmit = '#add_new_attribute';

        validate_new_attributes = function()
        {
            //FORM VALIDATION
            jQuery(formToSubmit).validate({
                    rules: {
                        /*newObjectDialog_object_type: { check_item_dropdown: true },
                        name: 'required' */
                    }, 
                    highlight: function(element) {
                        $(element).parent().parent().addClass("error");
                    },
                    unhighlight: function(element) {
                        $(element).parent().parent().removeClass("error");
                    },
                    submitHandler: function(form) {

                        //disable add button                            
                        $(formbutton).attr("disabled","disabled");

                        $(formToSubmit+' .status_info').html(loader_html());

                        var form = $(formToSubmit);
                        var form_data = form.serialize();

                        $.ajax({
                                url: form.attr('action'),
                                dataType: 'json',
                                type: "POST",
                                data: form_data,
                                success: function (data, textStatus, xhr)
                                {

                                    if(data.action==true){

                                        $(formToSubmit+' .status_info').html(alert_html(data.msg,'success')); 

                                        function action_delay(){
                                            $('.'+dialogClass).dialog("close");  
                                        }

                                        $('#attribute_id').append(
                                            $('<option></option>').val(data.id).html($('input[name="attribute_name[]"]').val())
                                        );

                                        $('#attribute_id').val(data.id)

                                        window.setTimeout(function() { action_delay(); }, 1001);

                                    }else{

                                        $(formbutton).removeAttr("disabled");
                                        $(formToSubmit+' .status_info').html(alert_html(data.msg,'error'));
                                        $(formToSubmit).animate({
                                                scrollTop:  $(formToSubmit+' .status_info').offset().top
                                            }, 200);
                                    }
                                },
                                error: function (xhr, textStatus, errorThrown) {
                                    //enable add button                            
                                    $(formbutton).removeAttr("disabled");
                                    $(formToSubmit+' .status_info').html(alert_html('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n','error'));
                                }
                        });
                    },
                    errorPlacement: function(error, element){}
            })
        }

        test_view_new_attribute = function (group_id,elem)

        {
            var url = base_url+'attributes/view_new_attribute/'+group_id;

            var buttons = [
                {
                    id: 'addNewAttribute',
                    text: "OK",
                    click: function() { jQuery(formToSubmit).trigger('submit'); }
                }
            ];

            var DialogOnOpen = validate_new_attributes;

            newDialog(url,dialogClass,dialogTitle,buttons,DialogOnOpen,567,590)
        }

        $('#opne_new_attribute').live('click',function(){
                group_id = $(this).attr('data-holder');
                if(group_id=='')
                    {
                    alert('Select type first')
                }else
                    {
                    //myObject['groupReference'] = this;
                    test_view_new_attribute(group_id,null);   
                }
        })


})